<template>
  <div id='shop'>
    <v-titletop>
      <span
        class='titleh'>商品名称:</span>
      <el-input
        v-model='productName'
        placeholder="请输入内容">
      </el-input>
      <span
        class='titleh'>商品描述:</span>
      <el-input
        placeholder="请输入内容"
        v-model='productDesc'>
      </el-input>
      <el-button
        type="primary"
        @click='search'>查找
      </el-button>
    </v-titletop>
    <v-titlebottom>
      <el-button
        type="primary"
        @click='add'>
        添加商品
      </el-button>
      <template style='auto'>
        <el-table
          :data="shoplist"
          height="auto" border
          style="width: 98.5%">
          <el-table-column
            label="商品ID"
            prop="_id"
            width="80">
          </el-table-column>
          <el-table-column
            label="商品名称"
            prop='name'
            width="180">
          </el-table-column>
          <el-table-column
            prop='desc'
            label="商品描述">
          </el-table-column>
          <el-table-column
            prop='price'
            label="商品价格">
          </el-table-column>
          <el-table-column
            label="状态">
            <template
              slot-scope="scope">
              <el-button
                size="mini"
                class='btn'
                @click="change(scope.$index, scope.row)">
                修改状态
              </el-button>
            </template>
          </el-table-column>
          <el-table-column
            label="操作">
            <template
              slot-scope="scope">
              <el-button
                size="mini"
                @click="info(scope.$index, scope.row)">
                详情
              </el-button>
              <el-button
                size="mini"
                @click="handlechange(scope.$index, scope.row)">
                修改
              </el-button>
            </template>
          </el-table-column>
        </el-table>
      </template>
      <div class="block">
        <el-pagination
          background
          @current-change="handleCurrentChange"
          :current-page.sync="currentPage"
          :page-size="pageSize"
          layout="prev, pager, next, jumper"
          :total="total">
        </el-pagination>
      </div>
    </v-titlebottom>
    <keep-alive>
      <component :is='comname'
        v-if='flag'
        @func='send'
        @get='getshoplist'
        :cominfo='cominfo'
        :shopinfo='shopinfo'>
      </component>
    </keep-alive>
  </div>
</template>

<script>
export default {
  name: 'shop',
  components: {
    'v-titletop': () => import('@/components/title-top'),
    'v-titlebottom': () => import('@/components/title-bottom'),
    addshop: () => import('@/components/commons/addshop'),
    shopinfo: () => import('@/components/commons/shopinfo'),
    shopchange: () => import('@/components/commons/shopchange'),
  },
  data() {
    return {
      pageNum: 1,
      pageSize: 10,
      shoplist: [],
      total: 0,
      currentPage: 1,
      stateflag: true,
      flag: false,
      cominfo: '',
      comname: '',
      productName: '',
      productDesc: '',
      shopinfo: {},
    }
  },
  methods: {
    getshoplist() {
      this.$axios({
        method: 'get',
        url: '/manage/product/list',
        params: {
          pageNum: this.pageNum,
          pageSize: this.pageSize,
        },
      }).then((res) => {
        this.shoplist = res.data.data.list
        for (let key of this.shoplist) {
          let str = '￥'
          str += key.price
          key.price = str
        }
        this.total = res.data.data.total
      })
    },
    handleCurrentChange(val) {
      this.pageNum = val
    },
    change(index, row) {
      this.$axios({
        method: 'post',
        url: '/manage/product/updateStatus',
        Headers: {
          'Content-Type': 'application/x-www-form-urlencoded',
        },
        params: {
          productId: row.categoryId,
          status: row.status,
        },
      }).then((res) => {
        console.log(res)
        if (res.data.status == '0') {
          if (res.statusText == 'OK') {
            this.$alert('已上架', '提示')
          } else {
            this.$alert('已下架', '提示')
          }
        }
      })
    },
    add() {
      this.cominfo = '添加商品'
      this.flag = true
      this.comname = 'addshop'
    },
    send(data) {
      this.flag = data
    },
    search() {
      this.$axios({
        method: 'get',
        url: '/manage/product/search',
        params: {
          pageNum: this.pageNum,
          pageSize: 5,
          productName: this.productName,
          productDesc: this.productDesc,
        },
      }).then((res) => {
        console.log(res)
        this.shoplist = res.data.data.list
        for (let key of this.shoplist) {
          let str = '￥'
          str += key.price
          key.price = str
        }
      })
      this.productName = ''
      this.productDesc = ''
    },
    info(index, row) {
      this.cominfo = '商品详情'
      this.flag = true
      this.comname = 'shopinfo'
      this.shopinfo = row
    },
    handlechange(index, row) {
      this.shopinfo = row
      this.flag = true
      this.cominfo = '修改商品信息'
      this.comname = 'shopchange'
    },
  },
  created() {
    this.getshoplist()
  },
}
</script>

<style lang='less'>
#shop {
  margin: 10px;
  .btn {
    border-radius: 4px;
    width: 60px;
    height: 30px;
    border: none;
    cursor: pointer;
    color: #fff;
    background-color: rgba(102, 177, 255);
    text-align: center;
    line-height: 30px;
    outline: none;
  }
  .btn:hover {
    background-color: rgba(102, 177, 255, 0.8);
  }
  #titletop,
  #titlebottom {
    .block {
      text-align: center;
      margin-bottom: 20px;
      margin-top: 20px;
      .el-input {
        width: 60px;
        input {
          width: 100%;
        }
      }
    }
  }
}
</style>